package
{
	import flash.display.DisplayObject;
	import flash.display.Sprite;
	import flash.events.Event;
	import flash.net.URLLoader;
	import flash.net.URLRequest;
	import flash.system.ApplicationDomain;
	import flash.system.LoaderContext;
	import flash.system.SecurityDomain;
	
	import game.define.typedef.MouseStyle;
	import game.manager.ConfigManager;
	
	import org.bulkLoader.comply.BulkLoader;
	import org.bulkLoader.comply.BulkLoaderDefine;
	import org.bulkLoader.events.BulkEvent;
	
	import utiles.MouseCursorHelper;
	import utiles.Reflection;
	
	[SWF(frameRate="24",height="600",width="800",backgroundColor="0")]
	public class GL extends Sprite
	{
		private var loader:BulkLoader = null;
		private var container:Sprite = null;
		
		public function GL()
		{
			container = new Sprite();
			addChild(container);
			
//			MouseCursorHelper.instance.registerStyle(MouseStyle.DEFAULT, new MC_DefaultMouseStyle());
//			MouseCursorHelper.instance.layer = container;
//			MouseCursorHelper.instance.show(MouseStyle.DEFAULT);
			
			loadConfig("assets/config.xml");
		}
		
		private function loadConfig(url:String):void
		{
			trace("load config  " + url);
			var loader:URLLoader = new URLLoader();
			loader.addEventListener(Event.COMPLETE, loadConfigCompleted);
			loader.load(new URLRequest(url));
		}
		
		protected function loadConfigCompleted(event:Event):void
		{
			trace("load config completed");
			
			var loader:URLLoader = event.target as URLLoader;
			loader.removeEventListener(Event.COMPLETE, loadConfigCompleted);
			
			var data:XML = new XML(event.target.data);
			ConfigManager.instance.setData(data);
			
			loadModules();
//			connectServer();
		}
		
		/*private function connectServer():void
		{
			var host:String = ConfigManager.instance.servHost;
			var port:uint = ConfigManager.instance.servPort;
			
			trace("connect server " + host + "[" + port + "]");
		}
		
		private function onConnectServerResult(ret:int):void
		{
			trace("connect server result:" + ret);
			switch (ret)
			{
				case 0:
					// allow login
					waltLogin();
					break;
			}
		}
		
		private function waltLogin():void
		{
			trace("client to load ui for user login, and walt user to login game.");
		}
		
		private function login(userName:String, userPassword:String):void
		{
			trace("login game, name:" + userName + ", password:" + userPassword);
		}
		
		private function onLoginResult(ret:int):void
		{
			switch (ret)
			{
				case 0: // succesful
					loadModules();
					break;
			}
		}*/
		
		private function loadModules():void
		{
			var loaderContent:LoaderContext = new LoaderContext(
				false, 
				ApplicationDomain.currentDomain,
				SecurityDomain.currentDomain
			);
			
			loader = new BulkLoader("bulkLoader");
			loader.addEventListener(BulkEvent.BULK_COMPLETED, loadModulesCompleted);
			
			var modules:Object = ConfigManager.instance.getModules();
			for each(var module:Object in modules)
			{
				loader.add(module.url, module);
				if (module.title == "configData")
					module[BulkLoaderDefine.ITEM_MINETYPE] = BulkLoaderDefine.FILE_BINARY;
			}
			loader.execute();
		}
		
		protected function loadModulesCompleted(event:Event):void
		{
			loader.removeEventListener(BulkEvent.BULK_COMPLETED, loadModulesCompleted);
			
			var main:DisplayObject = Reflection.createInstance("Main");
			addChild(main);
		}
	}
}